export const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

// 消息模版
export const pushTemplate = date => {
	// 前端代码：用户点击按钮触发订阅
	return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		uni.requestSubscribeMessage({
			tmplIds: ['5cli9R2v6CBve4e2ry6Ud5q_gxA2rb4eUeMLaLCni6g'], // 需要申请的模板ID
			success(res) {
				console.log(res, 'res')
				if (res['5cli9R2v6CBve4e2ry6Ud5q_gxA2rb4eUeMLaLCni6g'] === 'accept') {
					// 订阅成功，后端可推送消息
					resolve()
				} else {
					toast()
				}
			},
			fail(err) {
				toast()
			}
		})
		// #endif
		// #ifdef MP-ALIPAY
		resolve()
		// #endif

	})
}

const toast = () => {
	uni.showModal({
		title: '提示',
		content: '是否打开通知？',
		success: function(res) {
			if (res.confirm) {
				uni.openSetting({
					success(res) {
						console.log('已打开的设置项：', res.authSetting)

					},
					fail(err) {
						console.error('打开设置失败：', err)
					}
				})
				uni.hideLoading();
			} else if (res.cancel) {
				uni.hideLoading();
				console.log('用户点击取消');
			}
		}
	});

}


// export default {
// 	formatTime,
// 	pushTemplate
// }